//
//  FKIncomeDetailVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/14.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKIncomeDetailVC.h"
#import <JXCategoryView.h>
#import "FKIncomDetailListVC.h"
#import "FKDatePicker.h"
#import <PGDatePicker.h>
#import <PGDatePickManager.h>

@interface FKIncomeDetailVC ()<JXCategoryViewDelegate>

@property (nonatomic, weak) JXCategoryTitleView *categoryView;

/** 保存上次选中的时间 */
@property (nonatomic, strong) NSDateComponents *selectedDateComponents;

@end

@implementation FKIncomeDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收益明细";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fk_itemWithImageName:@"detail_icon_data_s" highImageName:@"detail_icon_data_s" target:self action:@selector(openDatePicker)];
    
    NSArray *titles = @[@"全部", @"已付款", @"已结算", @"已失效"];
    
    // 默认为user类型
    CGFloat headerH = fk_adjustW(44);
    JXCategoryTitleView *categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, headerH)];
    [self.view addSubview:categoryView];
    
    UIView *separator = [UIView fk_lineWithFrame:CGRectMake(0, headerH - 0.8, fkScreenW, 0.8)];
    [self.view addSubview:separator];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    scrollView.pagingEnabled = true;
    scrollView.showsHorizontalScrollIndicator = false;
    scrollView.bounces = false;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(separator.mas_bottom);
        make.left.bottom.right.offset(0);
    }];
    
    self.categoryView = categoryView;
    categoryView.titleSelectedColor = fkMainColor;
    categoryView.delegate = self;
    categoryView.titleColorGradientEnabled = YES;
    categoryView.titles = titles;
    categoryView.backgroundColor = [UIColor whiteColor];
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    categoryView.indicators = @[lineView];
    lineView.indicatorLineViewColor = fkMainColor;
    
    categoryView.contentScrollView = scrollView;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag | UIScrollViewKeyboardDismissModeInteractive;
    
    CGFloat scrollH = fkScreenH - fkStatusAndNaBarH - headerH;
    for (NSInteger index = 0; index < titles.count; index++) {
        // FKConsultListVC不能继承自UITableViewController，否则会导致宽度无法改变
        FKIncomDetailListVC *vc = [[FKIncomDetailListVC alloc] init];
        [self addChildViewController:vc];
//        vc.type = self.type;
        vc.status = fkFORMAT(@"%zd", index);
        vc.view.frame = CGRectMake(fkScreenW * index, 0, fkScreenW, scrollH);
        [scrollView addSubview:vc.view];
        
        // 让第一个控制器加载数据
//        if ([self.status isEqualToString:vc.status]) {
//            [vc refreshData];
//            categoryView.defaultSelectedIndex = index;
//            [vc refreshData];
//        } else {
            if (index == 0) [vc refreshData];
//        }
    }
    scrollView.contentSize = CGSizeMake(fkScreenW * titles.count, 0);
}

- (void)openDatePicker
{
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    //设置半透明的背景颜色
    datePickManager.isShadeBackgroud = true;
    //设置头部的背景颜色
    datePickManager.headerViewBackgroundColor = [UIColor fk_colorWithHex:0xF1F1F1];
    //设置确定按钮的字体颜色
    datePickManager.confirmButtonTextColor = fkMainColor;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.datePickerMode = PGDatePickerModeYearAndMonth;
    datePicker.maximumDate = [NSDate date];
    datePicker.language = @"zh-Hans";
    datePicker.middleTextColor = fkMainColor;
    datePicker.lineBackgroundColor = fkMainColor;
    datePicker.textColorOfSelectedRow = fkMainColor;
    if (self.selectedDateComponents) {
        // 显示上次选中的月份
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setYear:self.selectedDateComponents.year];
        [components setMonth:self.selectedDateComponents.month];
        [components setDay:1];
        [components setHour:1];
        [components setMinute:1];
        [components setSecond:1];
        NSDate *date = [calendar dateFromComponents:components];
        [datePicker setDate:date];
    }
    fkWeakSelf(self);
    datePicker.selectedDate = ^(NSDateComponents *dateComponents) {
        weakself.selectedDateComponents = dateComponents;
        NSString *monthStr = fkFORMAT(@"%zd-%02zd", dateComponents.year, dateComponents.month);
        for (FKIncomDetailListVC *vc in weakself.childViewControllers) {
            vc.month = monthStr;
        }
        FKIncomDetailListVC *currentChildVC = weakself.childViewControllers[weakself.categoryView.selectedIndex];
        [currentChildVC.tableView.mj_header beginRefreshing];
    };
    [self presentViewController:datePickManager animated:false completion:nil];
    
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    FKIncomDetailListVC *vc = self.childViewControllers[index];
    [vc refreshData];
}



@end
